/*
 * Copyright (c) 2008, Ramiro Espada and Jorge Hernandez
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
import org.molamil.cascada.selector.MalformedCriteriaError;
import org.molamil.cascada.selector.InvalidCriteriaError;
import org.molamil.cascada.selector.TypeCriteria;
import org.molamil.cascada.selector.TypeSelector;

import asunit.framework.TestCase;
 
/**
 * @author Jorge Hernandez and Ramiro Espada
 */
class org.molamil.cascada.selector.TypeSelectorTest extends TestCase {
	
	
	private var className:String = "org.molamil.cascada.selector.TypeSelectorTest";
	private var instance:TypeSelector;

	
	public function setUp():Void {
		instance = new TypeSelector();
	}

	public function tearDown():Void {
		delete instance;
	}

	public function testInstantiated():Void {
		assertTrue("TypeSelector instantiated", instance instanceof TypeSelector);
	}
	
	public function testSelect():Void {
		
		// First test set - testing standard type selection with constructors and TypeCriteria errors.
		var container:MovieClip = _root.createEmptyMovieClip("container", _root.getNextHighestDepth());
		
		container.createEmptyMovieClip("mc1", container.getNextHighestDepth());
		container.string1 = new String("string1");		container.createEmptyMovieClip("mc2", container.getNextHighestDepth());
		container.createTextField("texfield", container.getNextHighestDepth(), 0, 0, 10, 10);
		container.number1 = new Number(1);		container.createEmptyMovieClip("mc3", container.getNextHighestDepth());		container.number2 = new Number(2);		container.string2 = new String("string2");
		
		var result:Object;
		var criteria:TypeCriteria = new TypeCriteria();
		criteria.type = MovieClip;
		var selector:TypeSelector = new TypeSelector();
		selector.input = container;		selector.criteria = criteria;
		result = selector.select();
		
		assertTrue("3 MovieClips", oLength(result) == 3);
		assertTrue("MovieClip type 1", result.mc1 instanceof MovieClip);		assertTrue("MovieClip type 2", result.mc2 instanceof MovieClip);		assertTrue("MovieClip type 3", result.mc3 instanceof MovieClip);
		
		criteria.type = String;
		result = selector.select();
		
		assertTrue("2 Strings", oLength(result) == 2);
		assertTrue("String type 1", result.string1 instanceof String);
		assertTrue("String type 2", result.string2 instanceof String);
		
		criteria.type = Number;
		result = selector.select();
		
		assertTrue("2 Numbers", oLength(result) == 2);
		assertTrue("Numbers type 1", result.number1 instanceof Number);
		assertTrue("Numbers type 2", result.number2 instanceof Number);
		
		criteria.type = Boolean;
		result = selector.select();
		
		assertTrue("0 Booleans", oLength(result) == 0);
		
		selector.criteria = null;
		try {
			result = selector.select();
			fail("Null criteria didn't throw an error");
		} catch (e:InvalidCriteriaError) {}
		
		selector.criteria = new TypeCriteria();
		try {
			result = selector.select();
			fail("TypeCriteria with no type attribute didn't throw an error");
		} catch (e:MalformedCriteriaError) {}
		
		try {
			criteria.type = null;
			fail("TypeCriteria with null type attribute didn't throw an error");
		} catch (e:MalformedCriteriaError) {}
		
		try {
			criteria.type = "Array";
			fail("TypeCriteria with \"Array\" type attribute didn't throw an error");
		} catch (e:MalformedCriteriaError) {}
		
		try {
			criteria.type = 5;
			fail("TypeCriteria with 5 type attribute didn't throw an error");
		} catch (e:MalformedCriteriaError) {}
		
		// Second test set - testing primitives and properties created without constructors.
		var o:Object = new Object();
		o.a1 = [1, 2, 3];		o.a2 = new Array(1, 2, 3);
		o.s1 = "s1";		o.s2 = new String("s2");		o.n1 = 1;		o.n2 = new Number(2);		o.b1 = true;		o.b2 = new Boolean(false);
		
		var result2:Object;
		var criteria2:TypeCriteria = new TypeCriteria();
		criteria2.type = Array;
		var selector2:TypeSelector = new TypeSelector();
		selector2.input = o;
		selector2.criteria = criteria2;
		result2 = selector2.select();
		
		assertTrue("2 Arrays", oLength(result2) == 2);
		
		criteria2.type = "string";
		result2 = selector2.select();
		assertTrue("1 string (primitive)", oLength(result2) == 1);
		assertTrue("string (primitive) equals", result2.s1 == "s1");
		
		criteria2.type = String;
		result2 = selector2.select();		assertTrue("1 String", oLength(result2) == 1);		assertTrue("String equals", result2.s2 == "s2");
		
		criteria2.type = "number";
		result2 = selector2.select();
		assertTrue("1 number (primitive)", oLength(result2) == 1);
		assertTrue("number (primitive) equals", result2.n1 == 1);
		
		criteria2.type = Number;
		result2 = selector2.select();
		assertTrue("1 Number", oLength(result2) == 1);
		assertTrue("Number equals", result2.n2 == 2);
		
		criteria2.type = "boolean";
		result2 = selector2.select();
		assertTrue("1 boolean (primitive)", oLength(result2) == 1);
		assertTrue("boolean (primitive) equals", result2.b1 == true);
		
		criteria2.type = Boolean;
		result2 = selector2.select();
		assertTrue("1 Boolean", oLength(result2) == 1);
		assertTrue("Boolean equals", result2.b2 == false);
		
	}
	
	private function oLength(o:Object):Number {
		var n:Number = 0;
		for (var p:String in o)
			n++;
		return n;
	}
	
	
}
